import http from "@/http";
import { HistoricalQuotation, HistoricalQuotationQuery } from './type';

// 分页查询历史报价单列表
export const getHistoricalQuotationListApi = (params: HistoricalQuotationQuery) => {
  return http.get("/historicalQuotation/queryHistoricalQuotationList", params);
};

// 根据ID获取历史报价单详情
export const getHistoricalQuotationDetailApi = (id: number) => {
  return http.get(`/historicalQuotation/getHistoricalQuotationDetail/${id}`);
};

// 保存结构件报价单
export const saveStructureQuotationApi = (data: any) => {
  return http.post("/historicalQuotation/saveStructureQuotation", data);
}; 

// 根据项目ID获取报价单数据
export const getQuotationDataByProjectIdApi = (id: number) => {
  return http.get(`/historicalQuotation/quotation/project/${id}`);
};

export const getQuotationDataFromPricingModulesApi = (id: number) => {
  return http.get(`/historicalQuotation/getQuotationDataFromPricingModules/${id}`);
};

// 导出历史报价单列表
export const exportHistoricalQuotationApi = (params?: HistoricalQuotationQuery) => {
  // 构建查询字符串
  const queryString = params ? new URLSearchParams(
    Object.entries(params).reduce((acc, [key, value]) => {
      if (value !== undefined && value !== null && value !== '') {
        acc[key] = String(value);
      }
      return acc;
    }, {} as Record<string, string>)
  ).toString() : '';
  
  const url = `${http['instance'].defaults.baseURL}/historicalQuotation/export${queryString ? '?' + queryString : ''}`;
  window.location.href = url;
  return Promise.resolve();
};

